﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using IWshRuntimeLibrary;//Windows Script Host Object Model c:\windows\syswow64\wshom.ocx

namespace VideoClipNameHelper
{
    public class ShellLink
    {
        private static object o = new object();
        public static bool Create(string shortcutPathNameWithoutExt, string sourcePathName)
        {
            try
            {
                lock (o)
                {
                    //
                    string lnkPathName = shortcutPathNameWithoutExt + ".lnk";
                    //
                    if (System.IO.File.Exists(lnkPathName))
                    {
                        System.IO.File.Delete(lnkPathName);
                    }
                    //
                    WshShell shell = new WshShell();
                    IWshShortcut link = (IWshShortcut)shell.CreateShortcut(lnkPathName);
                    link.TargetPath = sourcePathName;
                    link.WindowStyle = 1;
                    //link.Description = description;
                    link.WorkingDirectory = Environment.SpecialFolder.MyDocuments.ToString();
                    link.IconLocation = sourcePathName + ",0";
                    link.Save();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"ERROR: can't create shortcut [{shortcutPathNameWithoutExt}/{sourcePathName}]): {e.Message}.");
                CLogger.Write(string.Format($"ERROR: can't create shortcut [{shortcutPathNameWithoutExt}/{sourcePathName}]): {e.Message}."));
                return false;
            }
            //
            return true;
        }
    }//class
}
